#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5, M = 2e5 + 5, K = 2005;
ll T, n, a[N], g[M][12][2], h[K][K][2];

void solve1() {
    memset(g, -0x3f, sizeof g);
    g[0][0][0] = g[0][0][1] = 0;
    ll ans = -1e18;
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j <= 10; j++) {
            g[i][j][0] = max(g[i][j][0], g[i - 1][j][0] + (a[i - 1] == a[i]) * a[i]);
            g[i][j][1] = max(g[i][j][1], g[i - 1][j][1] + (a[i - 1] == a[i]) * a[i]);
            g[i][a[i - 1]][0] = max(g[i][a[i - 1]][1], g[i - 1][j][1] + (j == a[i]) * a[i]);
            g[i][a[i - 1]][1] = max(g[i][a[i - 1]][1], g[i - 1][j][0] + (j == a[i]) * a[i]);
        }
    }
    for(int j = 0; j <= 10; j++)
        for(int k = 0; k <= 1; k++)
            ans = max(ans, g[n][j][k]);
    cout << ans << endl;
}
void solve2() {
    memset(h, -0x3f, sizeof h);
    h[0][0][0] = h[0][0][1] = 0;
    ll ans = -1e18;
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j <= 2000; j++) {
            h[i][j][0] = max(h[i][j][0], h[i - 1][j][0] + (a[i - 1] == a[i]) * a[i]);
            h[i][j][1] = max(h[i][j][1], h[i - 1][j][1] + (a[i - 1] == a[i]) * a[i]);
            h[i][a[i - 1]][0] = max(h[i][a[i - 1]][1], h[i - 1][j][1] + (j == a[i]) * a[i]);
            h[i][a[i - 1]][1] = max(h[i][a[i - 1]][1], h[i - 1][j][0] + (j == a[i]) * a[i]);
        }
    }
    for(int j = 0; j <= 2000; j++)
        for(int k = 0; k <= 1; k++)
            ans = max(ans, h[n][j][k]);
    cout << ans << endl;
}

int main() {
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    cin >> T;
    while(T--) {
        cin >> n;
        ll mx = 0;
        for(int i = 1; i <= n; i++) cin >> a[i], mx = max(mx, a[i]);
        if(n <= 200000 && mx <= 10) solve1();
        else if(n <= 2000 && mx <= 2000) solve2();
        else cout << "-1\n";
    }
    return 0;
}
